<?php


namespace logicmodel;


use comservice\Response;
use datamodel\Nft;

class ConfigLogic
{

    /**
     * NFT介绍
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function nft(){
        $data['desc_content'] = config('site.desc_content');
        $data['description_content'] = config('site.description_content');
        $data['team_content'] = config('site.team_content');
        $nft = (new Nft())->where(['is_del'=>0,'is_show'=>1])->order(['order asc'])->select();
        $nft = collection($nft)->toArray();
        $data['nft'] = addWebSiteUrl($nft,['image']);
        return Response::success('success',$data);
    }

    /**
     * 公司介绍
     * @return array
     */
    public function company(){
        $data['company_telegram'] = config('site.company_telegram');
        $data['company_email'] = config('site.company_email');
        $data['company_wx'] = config('site.company_wx');
        $data['company_record'] = config('site.company_record');
        $data['company_name'] = config('site.company_name');
        $data['company_content'] = config('site.company_content');
        return Response::success('success',$data);
    }
    /**
     * 系统文本信息
     * @return array
     */
    public function helpContent(){
        $data['register_content'] = content(config('site.register_content'));
        $data['privacy_content'] = content(config('site.privacy_content'));
        $data['business_content']  = content(config('site.business_content'));
        $data['about_content'] = content(config('site.about_content'));
        $data['app_download'] = config('site.app_download');
        $data['is_trade'] = intval(config('site.is_trade'));
        return Response::success('success',$data);
    }

    /**
     * 客服配置
     * @return array
     */
    public function serverConfig(){
        $data['server_phone'] = config('site.server_phone');
        $data['server_wx_image'] = config('site.server_wx_image');
        $data['server_wx_number'] = config('site.server_wx_number');
        $data['wx_global_app_id'] = config('site.wx_global_app_id');     
        $data = addWebSiteUrl($data,['server_wx_image']);
        return Response::success('success',$data);
    }
    public  function addWebSiteUrl($array, $fields = [])
    {
        $url = 'https://'.$_SERVER['HTTP_HOST'];
        if(count($array) <= 0) return $array;
        if (count($array) == count($array, 1)){
            //一维数组
            if(count($fields) > 0){
                foreach ($fields as $v){
                    $content = str_replace('<img src="/uploads/', '<img src="'.$url.'/uploads/' , $array[$v]);
                    $content = str_replace('<video src="/uploads/', '<video src="'.$url.'/uploads/' , $content);
                    $array[$v] = $content;
                }
            }else{
                foreach ($array as $k=>&$v){
                    $array[$k] = str_replace('<img src="/uploads/', '<img src="'.$url.'/uploads/' , $v);
                }
            }
            return $array;
        }else{
            if(count($array) <= 0) return $array;
            foreach ($array as &$v1) {
                foreach ($fields as &$v2) {
                    if(!empty($v1[$v2])){
                        $v1[$v2] =  str_replace('/uploads/', $url.'/uploads/' , $v1[$v2]);
                    }
                }
            }
            return $array;
        }
    }
}